package munyorderweb;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import munyorderweb_DB.Change_Bean;
import munyorderweb_DB.DB_manager;

public class Table_ment extends HttpServlet {


	String sql;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		table(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		table(req,resp);
	}

	protected void table(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Connection con = null;
		sql = "select * from tables";

		try{
			//DB接続　SQL結果取得
			con = DB_manager.getConnection();
			Statement stm = con.createStatement();
			ResultSet rs = stm.executeQuery(sql);

			req.setCharacterEncoding("utf-8");
			resp.setContentType("text/html; charset=utf-8");

			//Change_Bean型でArrayListに投入
			Change_Bean change = null;
			ArrayList<Change_Bean> list = new ArrayList<Change_Bean>();
			while(rs.next()){
				change = new Change_Bean();
				change.setTable_no(rs.getInt("table_no"));
				change.setTable_name(rs.getString("table_name"));
				change.setTable_seats(rs.getInt("table_seats"));
				System.out.println(change.getTable_seats());
				list.add(change);
			}

			//表示用Jspファイルを呼出して画面上に表示
			req.setAttribute("Table_ment", list);
			RequestDispatcher dispatcher = req.getRequestDispatcher("Table_ment.jsp");
			dispatcher.include(req, resp);

		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				if(con != null){
					con.close();
				}
			}catch (SQLException e){
				e.printStackTrace();
			}
		}
	}
}
